﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MLM.AdminCP.Class;
using Telerik.Web.UI;
using MLM.Base;

namespace MLM.AdminCP.Pages
{
    public partial class ProductMananger :BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            Permission.CheckIsAdmin();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
            RadProductGrid.ItemUpdated += new GridUpdatedEventHandler(radProductGrid_ItemUpdated);
            RadProductGrid.ItemInserted += new GridInsertedEventHandler(radProductGrid_ItemInserted);
            RadProductGrid.ItemDeleted += new GridDeletedEventHandler(radProductGrid_ItemDeleted);
        }
        void radProductGrid_ItemDeleted(object sender, GridDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                Message.ShowError("Product " + e.Item["ProductBaseID"].Text + " cannot be deleted. Reason: " + e.Exception.Message);
            }
            else
            {
                Message.ShowSuccess("Product " + e.Item["ProductBaseID"].Text + " deleted!");
            }
        }

        void radProductGrid_ItemInserted(object sender, GridInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                Message.ShowError("Product cannot be inserted. Reason: " + e.Exception.Message);
                e.ExceptionHandled = true;
                e.KeepInInsertMode = true;
            }
            else
            {
                Message.ShowSuccess("Product inserted!");
            }
        }

        void radProductGrid_ItemUpdated(object sender, GridUpdatedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.KeepInEditMode = true;
                e.ExceptionHandled = true;
                Message.ShowError("Product " + e.Item["ProductBaseID"].Text + " cannot be updated. Reason: " + e.Exception.Message);
            }
            else
            {
                Message.ShowSuccess("Product " + e.Item["ProductBaseID"].Text + " updated.");
            }
        } 
    }
}